layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //菜单列表
    var tableIns = table.render({
        elem: '#menuList',
        url : Feng.ctxPath+'/sysMenu/menulist',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        id : "menuListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'id', width:60, align:"left" },
            {field: 'title', title: '菜单名称', align:"center"},
            {field: 'code', title: '菜单编号', align:"left"},
            {field: 'pcode', title: '菜单父级编号', align:"left"},
            {field: 'clazz', title: '层级', align:'left'},
            {field: 'icon', title: '图标', align:'left'},
            {field: 'href', title: '路径',  align:'left'},
            {field: 'ismenu', title: '是否菜单', align:'left',templet:'#ismenu'},
            /*{field: 'ismenu', title: '是否菜单', align:'left',templet:function(d){
                    return d.ismenu == "0" ? "是" : "不是";
            }},*/
            {field: 'spread', title: '是否展开下一级', align:'left'},
            {title: '操作', width:200, templet:'#menuListBar',fixed:"right",align:"center"}
        ]]
        ,done: function () {
            $("[data-field='id']").css('display','none');
        }

    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
            table.reload("menuListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
    });

    //添加菜单
    $(".addMenu_btn").click(function(){
        addMenu();
    })

    //添加菜单
    function addMenu(edit){
        var index = layui.layer.open({
            title : "添加菜单",
            type : 2,
            area: ['800px', '450px'] ,
            content : Feng.ctxPath+"/sysMenu/showMenuAdd"
        });
       // layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    function editMenu(data){
        var index = layui.layer.open({
            title : "编辑菜单",
            type : 2,
            area: ['800px', '450px'] ,
            content : Feng.ctxPath+"/sysMenu/showMenuAdd",
            success : function(layer, index){
                var body = layui.layer.getChildFrame('body', index);
                if(data){
                    body.find(".id").val(data.id);
                    body.find(".title").val(data.title);
                    body.find("#clazz").val(data.clazz);
                    body.find(".code").val(data.code);
                    body.find(".pcode").val(data.pcode);
                    body.find(".icon").val(data.icon);
                    body.find(".href").val(data.href);
                    body.find("#ismenu").val(data.ismenu);
                    form.render('select',"#clazz");

                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回菜单列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        // layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    //列表操作
    table.on('tool(menuList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            editMenu(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此菜单？',{icon:3, title:'提示信息'},function(index){
                var msg = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                $.post(Feng.ctxPath+"/sysMenu/delMenu",{"id":data.id},function(result){
                    if(result.success){
                        setTimeout(function(){
                            layer.close(msg);
                            layer.msg(result.message);
                            location.reload();
                            layer.close(index);
                        },500);
                    }else{
                        layer.close(msg);
                        layer.msg(result.message);
                    }
                },"json");
            });
        }
    });

})